// pages/webview/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    product:{},
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    province: app.globalData.province,
    city: app.globalData.city,
    county: app.globalData.county,
    fcode: '',
    vcode: '',
    xuliehao:'',
    goods_title:'',
    popShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showToast({
      title: '加载中..',
      icon: 'loading',
      //duration: 1500
    });
    var that = this
    that.setData({
      fcode: options.fcode, 
      xuliehao: options.fcode.substring(12, 20),    
      vcode: options.vcode,
      province: app.globalData.province,
      city: app.globalData.city,
      county: app.globalData.county,
    });
    this.getfcodedetect(options.fcode, options.vcode);
  },
  //数据查询
  getfcodedetect: function (fcode, vcode){
    wx.request({
      url: app.globalData.the_host + `/fcode/productbyfcode`,
      data: {
        "code": fcode,  
      },
      header: {
        'content-type': 'application/json'
      },
      method: "get",
      success: res => {       
        if (res.statusCode == 200) {
          if (res.data.shortName == undefined){            
              this.setData({
                popShow: true,
              })
            wx.showToast({
              title: '未找到产品',
              icon: 'success',
              duration: 5000,
            })
          }else{
            this.setData({
              product: res.data,
              goods_title: res.data.shortName,
            })
          }
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'success',
            duration: 2000,
          })
        }
        wx.hideToast();//隐藏提示框
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   *   //加载视频
   */
  onReady: function() {
    this.videoContext = wx.createVideoContext('myVideo')
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
   
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    that.fenxiang()
    var goods_id = 1;//获取产品id
    var goods_title = that.data.goods_title;//获取产品标题
    // var goods_img = 'http://101.132.97.213:9090/image/1233212212212/1575952652583827.jpg';//产品图片
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: goods_title,
        path: '/pages/webview/index?fcode=' + that.data.fcode + "&vcode=" + that.data.vcode,
        //imageUrl: goods_img //不设置则默认为当前页面的截图
      }
    }
  },
  //提交分享
  fenxiang: function () {
    wx.request({
      // url: app.globalData.the_host + '/newcode/api/scan2.htm',
      url: app.globalData.the_host + `/fcode/wechatshare`,
      data: {
        "city": app.globalData.city,             // 可选
        "province": app.globalData.province,         // 可选
        "address": app.globalData.county,         // 可选
        "lat": app.globalData.lat,
        "lng": app.globalData.lng,
        "openId": app.globalData.openId,//必填
      },
      header: {
        'content-type': 'application/json'
      },
      method: "POST",
      success: res => {
        if (res.statusCode == 200) {
          if (res.data.status == 0) {
            console.log(res.data)
          }
        }
      }
    })
  },
})